package atag;

import atag.common.ATaGProgram;
import atag.gui.MainFrame;
import org.apache.log4j.*;

import javax.swing.*;
import java.io.*;
import Tools.Executer;
import Tools.ExecuterStandard;
import atag.compiler.ATaGParser;

/**
 * Created by IntelliJ IDEA.
 * User: home
 * Date: Jan 21, 2011
 * Time: 3:27:03 PM
 * To change this template use File | Settings | File Templates.
 */
public class SrijanFactory {

    ATaGProgram atg_program = new ATaGProgram();
    static MainFrame m_frame;

    public ATaGProgram getAtg_program() {
        return atg_program;
    }

    public void setAtg_program(ATaGProgram atg_program) {
        this.atg_program = atg_program;
    }

    public static MainFrame getM_frame() {
        return m_frame;
    }

    public static void setM_frame(MainFrame m_frame) {
        SrijanFactory.m_frame = m_frame;
    }

    public SrijanFactory(ATaGProgram atg_program) {
        this.atg_program = atg_program;
        m_frame = new MainFrame(getAtg_program());
    }
   
    public static void main(String[] args) {

        if (args.length == 2) { 
            // Working Directory and Task Graph specified
            // Launches GUI 
            final String workingDir=args[0];
            final String taskGraphPath=args[1];            
            final ATaGProgram ap = ATaGParser.parseATaGProgram(taskGraphPath);            
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    atag.gui.MainFrame mf = new atag.gui.MainFrame(ap,workingDir);
                    mf.setVisible(true);
                }
            });
        } else if (args.length == 4) { 
            // Working Directory, Task Graph, Network Description and Flag specified 
            // Does not launch GUI 
            final String workingDir=args[0];
            final String taskGraphPath=args[1];
            final String networkDescPath=args[2];
            final String compilerFlag=args[3];
            ATaGProgram ap = ATaGParser.parseATaGProgram(taskGraphPath);            
            String[] compilerArgs = {taskGraphPath, networkDescPath,compilerFlag,
                "random", "0", workingDir};
            atag.compiler.Main.main(compilerArgs, ap);
        } else {
            System.err.println("Usage: java -jar Srijan.jar <Path to Working Directory>,"
                    + " <Path to Task Graph XML File>");
            System.exit(-1);
        }        
    }
}
